package com.snow.storeapi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.snow.storeapi.entity.DiagnosisResult;
import com.snow.storeapi.entity.PatientInfo;
import com.snow.storeapi.entity.R;
import com.snow.storeapi.enums.RecordState;
import com.snow.storeapi.service.IDiagnosisResultService;
import com.snow.storeapi.service.IPatientInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/patients")
public class PatientInfoController {
    @Autowired
    private IPatientInfoService patientInfoService;

    @Autowired
    private IDiagnosisResultService diagnosisResultService;

    @GetMapping("/{recordId}")
    public PatientInfo selectByRecordId(@PathVariable String recordId) {
        PatientInfo patientInfo = patientInfoService.selectByRecordId(recordId);
        return patientInfo;
    }
    @GetMapping("/all")
    public List<PatientInfo> selectAllPatients() {
        List<PatientInfo> patientInfos = patientInfoService.selectAllPatients();
        return patientInfos;
    }

    // 保存病历
    @PostMapping("/save/{recordId}")
    public R saveRecord(@RequestBody PatientInfo patientInfo, @PathVariable String recordId) {
        PatientInfo updateData = new PatientInfo();
        updateData.setRecordState(RecordState.SAVE.getTypeName());
        updateData.setRecordDocId(patientInfo.getRecordDocId());
        patientInfoService.update(updateData, new QueryWrapper<PatientInfo>().eq("record_id", recordId));
        return R.ok();
    }

    // 作废病历
    @PostMapping("/cancel/{recordId}")
    public R cancelRecord(@PathVariable String recordId) {
        PatientInfo patientInfo = new PatientInfo();
        patientInfo.setRecordDocId("");
        patientInfo.setRecordState(RecordState.CANCEL.getTypeName());
        patientInfoService.update(patientInfo, new QueryWrapper<PatientInfo>().eq("record_id", recordId));
        return R.ok();
    }

    // 确诊病历
    @PostMapping("/confirm/{recordId}")
    public R confirmRecord(@PathVariable String recordId, @RequestBody Map<String, List<DiagnosisResult>> diagnosisResults) {
        PatientInfo patientInfo = new PatientInfo();
        patientInfo.setRecordState(RecordState.CONFIRM.getTypeName());
        patientInfo.setConfirmTime(LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss")));
        patientInfoService.update(patientInfo, new QueryWrapper<PatientInfo>().eq("record_id", recordId));

        // 更新计算出结果的状态
        if (diagnosisResults.get("computer") != null && diagnosisResults.get("computer").size() > 0) {
            diagnosisResultService.batchUpdateState(diagnosisResults.get("computer"), recordId);
        }
        // 若存在自选病历,则新增至诊断结果表
        if (diagnosisResults.get("manually") != null && diagnosisResults.get("manually").size() > 0) {
            diagnosisResultService.batchInsert(diagnosisResults.get("manually"), recordId);
        }
        return R.ok();
    }

    // 当日病历
    @GetMapping("/daily/{visitDate}")
    public List<PatientInfo> dailyPatients(@PathVariable String visitDate) {
        List<PatientInfo> records = patientInfoService.dailyPatients(visitDate);
        return records;
    }

    // 患病男女比例
    @GetMapping("/sexRate/{visitDate}")
    public List<Map<String, Object>> sexRate(@PathVariable String visitDate) {
        List<Map<String, Object>> data = patientInfoService.sexRate(visitDate);
        return data;
    }

}
